﻿-- CONFIGURATION SECTION  --  Feel free to change the values in this section to your liking.
bro1 = "Serese";
bro2 = "Jwh";
bro3 = "Pagodà";
bro4 = "Dwain";

-- bro1Alts = { }
-- bro1Alts[1] = "\"Serese\""
-- bro1Alts[2] = "Protocol"

-- function bro1Check()
	-- for i = 1, 2 do
		-- if(UnitExists(bro1Alts[i])) then
		-- local bro1 = bro1Alts[i]
		-- end
	-- end
-- end

-- Position of bars ( x, y )
xpos = -400
ypos = -130

-- Bar options
barWidth = 130
barHeight = 20
barSpacing = 35
barAlpha = .95           -- In range opacity value
oorbarAlpha = .3     -- Out of range opacity value
barBorder = -1     -- Offset into frame
hpBarTexturePath = "Interface\\Addons\\broBars\\"   --  Folder that contains custom textures
hpBarTexture = "dV.tga"       -- Minimalist.tga by default

-- Text options
fontPath = "Fonts\\FRIZQT__.TTF"
fontColor = {225/255, 225/255, 225/255}  --  r g b values
nameFontSize = 10
hpFontSize = 12
textStyle = ""   --  Options are "OUTLINE",  "THICKOUTLINE", or ""  -- double quotes no space for no outline

nameJustify = "RIGHT"
nameSide = "LEFT"

hpJustify = "LEFT"
hpSide = "LEFT"

-- Style preferences
useShortHP = true   -- Toggles short vs. long hp numbers
--useDebuffHighlighting = true
--usePowerBars = true

----------------------------------------------------------------------------------------------------------------
-- ADDON CODE STARTS HERE -- Leave it alone unless you know what you're doing.
-- Class coloring functions for bars
local _, class, c
function bro1Color()
	if(UnitExists(bro1)) then 
	_, class = UnitClass(bro1)
	c = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	bro1HealthBar:SetStatusBarColor(c.r, c.g, c.b)
	end
end

function bro2Color()
	if(UnitExists(bro2)) then 
	_, class = UnitClass(bro2)
	c = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	bro2HealthBar:SetStatusBarColor(c.r, c.g, c.b)
	end
end

function bro3Color()
	if(UnitExists(bro3)) then 
	_, class = UnitClass(bro3)
	c = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	bro3HealthBar:SetStatusBarColor(c.r, c.g, c.b)
	end
end

function bro4Color()
	if(UnitExists(bro4)) then 
	_, class = UnitClass(bro4)
	c = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	bro4HealthBar:SetStatusBarColor(c.r, c.g, c.b)
	end
end

-- Formatting shorter hp numbers -- ie 24800 into 24.8k
function round(num, idp)
  if idp and idp>0 then
    local mult = 10^idp
    return math.floor(num * mult + 0.5) / mult
  end
  return math.floor(num + 0.5)
end

function shortHP(num)
	if(num >= 1000) then
		return round(num/1000).."k"
	else
		return num
	end
end

-- Range checking
broBars_UpdateInterval = .25;
local timer = 0;
function broBars_OnUpdate(self, elapsed)
  timer = timer + elapsed; 	

	if (timer > broBars_UpdateInterval) then
			if UnitInRange(bro1) then
				bro1HealthBar:SetAlpha(barAlpha)
			elseif not UnitInRange(bro1) then
				bro1HealthBar:SetAlpha(oorbarAlpha)
			end
			
			if UnitInRange(bro2) then
				bro2HealthBar:SetAlpha(barAlpha)
			elseif not UnitInRange(bro2) then
				bro2HealthBar:SetAlpha(oorbarAlpha)
			end	
			
			if UnitInRange(bro3) then
				bro3HealthBar:SetAlpha(barAlpha)
			elseif not UnitInRange(bro3) then
				bro3HealthBar:SetAlpha(oorbarAlpha)
			end	
			
			if UnitInRange(bro4) then
				bro4HealthBar:SetAlpha(barAlpha)
			elseif not UnitInRange(bro4) then
				bro4HealthBar:SetAlpha(oorbarAlpha)
			end	
		timer = 0;
	end
end

-- Bar updating functions
function bro1UpdateHealth()
	local bro1currentHP = UnitHealth(bro1);
	local bro1maxHP = UnitHealthMax(bro1);
	if (useShortHP == true) then
		bro1percentHP = shortHP(bro1currentHP)
	else
		bro1percentHP = bro1currentHP
	end
	bro1HealthBar:SetMinMaxValues(0, bro1maxHP);
	bro1HealthBar:SetValue(bro1currentHP);
	bro1HealthText:SetText(bro1percentHP);
	bro1Color();
		if(UnitExists(bro1)) then 
			bro1HealthBar:Show();
			bro1Button:Show();
		else
			bro1HealthBar:Hide();
			bro1Button:Hide();
			
		end
end

function bro2UpdateHealth()
	local bro2currentHP = UnitHealth(bro2);
	local bro2maxHP = UnitHealthMax(bro2);
	if (useShortHP == true) then
		bro2percentHP = shortHP(bro2currentHP)
	else
		bro2percentHP = bro2currentHP
	end
	bro2HealthBar:SetMinMaxValues(0, bro2maxHP);
	bro2HealthBar:SetValue(bro2currentHP);
	bro2HealthText:SetText(bro2percentHP);
	bro2Color();
		if(UnitExists(bro2)) then 
			bro2HealthBar:Show();
			bro2Button:Show();
		else
			bro2HealthBar:Hide();
			bro2Button:Show();
		end
end

function bro3UpdateHealth()
	local bro3currentHP = UnitHealth(bro3);
	local bro3maxHP = UnitHealthMax(bro3);
	if (useShortHP == true) then
		bro3percentHP = shortHP(bro3currentHP)
	else
		bro3percentHP = bro3currentHP
	end
	bro3HealthBar:SetMinMaxValues(0, bro3maxHP);
	bro3HealthBar:SetValue(bro3currentHP);
	bro3HealthText:SetText(bro3percentHP);
	bro3Color();
		if(UnitExists(bro3)) then 
			bro3HealthBar:Show();
			bro3Button:Show();
		else
			bro3HealthBar:Hide();
			bro3Button:Show();
		end
end

function bro4UpdateHealth()
	local bro4currentHP = UnitHealth(bro4);
	local bro4maxHP = UnitHealthMax(bro4);
	if (useShortHP == true) then
		bro4percentHP = shortHP(bro4currentHP)
	else
		bro4percentHP = bro4currentHP
	end
	bro4HealthBar:SetMinMaxValues(0, bro4maxHP);
	bro4HealthBar:SetValue(bro4currentHP);
	bro4HealthText:SetText(bro4percentHP);
	bro4Color();
		if(UnitExists(bro4)) then 
			bro4HealthBar:Show();
			bro4Button:Show();
		else
			bro4HealthBar:Hide();
			bro4Button:Show();
		end
end

-- Setting up the anchor frame, status bars, name text, and hp text
function bro1CreateAnchor() -- bro1 Anchor Frame
	local bro1AnchorFrame=CreateFrame("Frame", "bro1AnchorFrame", UIParent);
	bro1AnchorFrame:SetWidth(barWidth); 
	bro1AnchorFrame:SetHeight(barHeight);
	bro1AnchorFrame:SetPoint("Center", UIParent, "Center", xpos, ypos); 
end

function bro2CreateAnchor() -- bro2 Anchor Frame
	local bro2AnchorFrame=CreateFrame("Frame", "bro2AnchorFrame", UIParent);
	bro2AnchorFrame:SetWidth(barWidth); 
	bro2AnchorFrame:SetHeight(barHeight);
	bro2AnchorFrame:SetPoint("Center", UIParent, "Center", xpos, ypos - barSpacing);
end

function bro3CreateAnchor() -- bro3 Anchor Frame
	local bro3AnchorFrame=CreateFrame("Frame", "bro3AnchorFrame", UIParent);
	bro3AnchorFrame:SetWidth(barWidth);
	bro3AnchorFrame:SetHeight(barHeight); 
	bro3AnchorFrame:SetPoint("Center", UIParent, "Center", xpos, ypos - barSpacing*2); 
end

function bro4CreateAnchor() -- bro4 Anchor Frame
	local bro4AnchorFrame=CreateFrame("Frame", "bro4AnchorFrame", UIParent);
	bro4AnchorFrame:SetWidth(barWidth);
	bro4AnchorFrame:SetHeight(barHeight);
	bro4AnchorFrame:SetPoint("Center", UIParent, "Center", xpos, ypos - barSpacing*3);
end

function bro1CreateHealthBar() -- bro1 Health Bar
	local bro1currentHP = UnitHealth(bro1);
	local bro1maxHP = UnitHealthMax(bro1);
	local bro1percentHP = shortHP(bro1currentHP);

	local bro1HealthBar=CreateFrame("StatusBar", "bro1HealthBar", bro1AnchorFrame);
	bro1HealthBar:SetPoint("Top", bro1AnchorFrame, "Bottom");
	bro1HealthBar:SetWidth(barWidth);
	bro1HealthBar:SetHeight(barHeight);
	bro1HealthBar:SetMinMaxValues(0, bro1maxHP);
	bro1HealthBar:SetValue(bro1currentHP);
	bro1HealthBar:SetStatusBarTexture(hpBarTexturePath..hpBarTexture);
	bro1HealthBar:SetBackdrop({
		bgFile="Interface\\Tooltips\\UI-Tooltip-Background", 
		tile=1, tileSize=10, edgeSize=6, 
		insets={left=barBorder, right=barBorder, top=barBorder, bottom=barBorder}});
	bro1HealthBar:SetBackdropColor(0, 0, 0, 1);
	-- hp text
	local bro1HealthText=bro1HealthBar:CreateFontString("bro1HealthText", "ARTWORK","GameFontNormal");
	bro1HealthText:SetFont(fontPath, hpFontSize, textStyle)
	bro1HealthText:SetPoint(hpJustify, bro1HealthBar, hpSide, 5, 0);
	bro1HealthText:SetText(bro1percentHP);
	bro1HealthText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	-- name text
	local bro1NameText = bro1HealthBar:CreateFontString("bro1NameText", "ARTWORK", "GameFontNormal");
	bro1NameText:SetFont(fontPath, nameFontSize, textStyle)
	bro1NameText:SetPoint(nameJustify, bro1HealthBar, nameSide, -3, 0);
	bro1NameText:SetText(bro1);
	bro1NameText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);

	-- Targeting function
	local bro1Button = CreateFrame("Button", "bro1Button", bro1AnchorFrame, "UIPanelButtonTemplate,SecureActionButtonTemplate"); 
	bro1Button:SetAttribute("type", "target"); 
	bro1Button:SetAttribute("target", "unit");
	bro1Button:SetAttribute("unit", bro1);
	bro1Button:SetAlpha(0);  -- set to 1 for debug
	bro1Button:SetWidth(barWidth); 
	bro1Button:SetHeight(barHeight); 
	bro1Button:ClearAllPoints(); 
	bro1Button:SetPoint("CENTER", bro1AnchorFrame, 0, -barHeight); 
end

function bro2CreateHealthBar() -- bro2 Health Bar
	local bro2currentHP = UnitHealth(bro2);
	local bro2maxHP = UnitHealthMax(bro2);
	local bro2percentHP = shortHP(bro2currentHP);

	local bro2HealthBar=CreateFrame("Statusbar", "bro2HealthBar", bro2AnchorFrame);
	bro2HealthBar:SetPoint("Top", bro2AnchorFrame, "Bottom");
	bro2HealthBar:SetWidth(barWidth);
	bro2HealthBar:SetHeight(barHeight);
	bro2HealthBar:SetMinMaxValues(0, bro2maxHP);
	bro2HealthBar:SetValue(bro2currentHP);
	bro2HealthBar:SetStatusBarTexture(hpBarTexturePath..hpBarTexture);
	bro2HealthBar:SetBackdrop({
		bgFile="Interface\\Tooltips\\UI-Tooltip-Background", 
		tile=1, tileSize=10, edgeSize=6, 
		insets={left=barBorder, right=barBorder, top=barBorder, bottom=barBorder}});
	bro2HealthBar:SetBackdropColor(0, 0, 0, 1);
	-- hp text
	local bro2HealthText=bro2HealthBar:CreateFontString("bro2HealthText", "ARTWORK","GameFontNormal");
	bro2HealthText:SetFont(fontPath, hpFontSize, textStyle)
	bro2HealthText:SetPoint(hpJustify, bro2HealthBar, hpSide, 5, 0);
	bro2HealthText:SetText(bro2percentHP);
	bro2HealthText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	
	-- name text
	local bro2NameText = bro2HealthBar:CreateFontString("bro2NameText", "ARTWORK", "GameFontNormal");
	bro2NameText:SetFont(fontPath, nameFontSize, textStyle)
	bro2NameText:SetPoint(nameJustify, bro2HealthBar, nameSide, -3, 0);
	bro2NameText:SetText(bro2);
	bro2NameText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	
	-- Targeting function
	local bro2Button = CreateFrame("Button", "bro2Button", bro2AnchorFrame, "UIPanelButtonTemplate,SecureActionButtonTemplate"); 
	bro2Button:SetAttribute("type", "target"); 
	bro2Button:SetAttribute("target", "unit");
	bro2Button:SetAttribute("unit", bro2);
	bro2Button:SetAlpha(0);   -- set to 1 for debug
	bro2Button:SetWidth(barWidth); 
	bro2Button:SetHeight(barHeight); 
	bro2Button:ClearAllPoints(); 
	bro2Button:SetPoint("CENTER", bro2AnchorFrame, 0, -barHeight); 
end

function bro3CreateHealthBar() -- bro3 Health Bar
	local bro3currentHP = UnitHealth(bro3);
	local bro3maxHP = UnitHealthMax(bro3);
	local bro3percentHP = shortHP(bro3currentHP);

	local bro3HealthBar=CreateFrame("Statusbar", "bro3HealthBar", bro3AnchorFrame);
	bro3HealthBar:SetPoint("Top", bro3AnchorFrame, "Bottom");
	bro3HealthBar:SetWidth(barWidth);
	bro3HealthBar:SetHeight(barHeight);
	bro3HealthBar:SetMinMaxValues(0, bro3maxHP);
	bro3HealthBar:SetValue(bro3currentHP);
	bro3HealthBar:SetStatusBarTexture(hpBarTexturePath..hpBarTexture);
	bro3HealthBar:SetBackdrop({
		bgFile="Interface\\Tooltips\\UI-Tooltip-Background", 
		tile=1, tileSize=10, edgeSize=6, 
		insets={left=barBorder, right=barBorder, top=barBorder, bottom=barBorder}});
	bro3HealthBar:SetBackdropColor(0, 0, 0, 1);
	-- hp text
	local bro3HealthText=bro3HealthBar:CreateFontString("bro3HealthText", "ARTWORK","GameFontNormal");
	bro3HealthText:SetFont(fontPath, hpFontSize, textStyle)
	bro3HealthText:SetPoint(hpJustify, bro3HealthBar, hpSide, 5, 0);
	bro3HealthText:SetText(bro3percentHP);
	bro3HealthText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	-- name text
	local bro3NameText = bro3HealthBar:CreateFontString("bro3NameText", "ARTWORK", "GameFontNormal");
	bro3NameText:SetFont(fontPath, nameFontSize, textStyle)
	bro3NameText:SetPoint(nameJustify, bro3HealthBar, nameSide, -3, 0);
	bro3NameText:SetText(bro3);
	bro3NameText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	
	-- Targeting function
	local bro3Button = CreateFrame("Button", "bro3Button", bro3AnchorFrame, "UIPanelButtonTemplate,SecureActionButtonTemplate"); 
	bro3Button:SetAttribute("type", "target"); 
	bro3Button:SetAttribute("target", "unit");
	bro3Button:SetAttribute("unit", bro3);
	bro3Button:SetAlpha(0);   -- set to 1 for debug
	bro3Button:SetWidth(barWidth); 
	bro3Button:SetHeight(barHeight); 
	bro3Button:ClearAllPoints(); 
	bro3Button:SetPoint("CENTER", bro3AnchorFrame, 0, -barHeight); 
end

function bro4CreateHealthBar() -- bro4 Health Bar
	local bro4currentHP = UnitHealth(bro4);
	local bro4maxHP = UnitHealthMax(bro4);
	local bro4percentHP = shortHP(bro4currentHP);

	local bro4HealthBar=CreateFrame("Statusbar", "bro4HealthBar", bro4AnchorFrame);
	bro4HealthBar:SetPoint("Top", bro4AnchorFrame, "Bottom");
	bro4HealthBar:SetWidth(barWidth);
	bro4HealthBar:SetHeight(barHeight);
	bro4HealthBar:SetMinMaxValues(0, bro4maxHP);
	bro4HealthBar:SetValue(bro4currentHP);
	bro4HealthBar:SetStatusBarTexture(hpBarTexturePath..hpBarTexture);
	bro4HealthBar:SetBackdrop({
		bgFile="Interface\\Tooltips\\UI-Tooltip-Background", 
		tile=1, tileSize=10, edgeSize=6, 
		insets={left=barBorder, right=barBorder, top=barBorder, bottom=barBorder}});
	bro4HealthBar:SetBackdropColor(0, 0, 0, 1);
	-- hp text
	local bro4HealthText=bro4HealthBar:CreateFontString("bro4HealthText", "ARTWORK","GameFontNormal");
	bro4HealthText:SetFont(fontPath, hpFontSize, textStyle)
	bro4HealthText:SetPoint(hpJustify,bro4HealthBar, hpSide, 5, 0);
	bro4HealthText:SetText(bro4percentHP);
	bro4HealthText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	-- name text
	local bro4NameText = bro4HealthBar:CreateFontString("bro4NameText", "ARTWORK", "GameFontNormal");
	bro4NameText:SetFont(fontPath, nameFontSize, textStyle)
	bro4NameText:SetPoint(nameJustify, bro4HealthBar, nameSide, -3, 0);
	bro4NameText:SetText(bro4);
	bro4NameText:SetTextColor(fontColor[1], fontColor[2], fontColor[3]);
	
	-- Targeting function
	local bro4Button = CreateFrame("Button", "bro4Button", bro4AnchorFrame, "UIPanelButtonTemplate,SecureActionButtonTemplate"); 
	bro4Button:SetAttribute("type", "target"); 
	bro4Button:SetAttribute("target", "unit");
	bro4Button:SetAttribute("unit", bro4);
	bro4Button:SetAlpha(0);   -- set to 1 for debug
	bro4Button:SetWidth(barWidth); 
	bro4Button:SetHeight(barHeight); 
	bro4Button:ClearAllPoints(); 
	bro4Button:SetPoint("CENTER", bro4AnchorFrame, 0, -barHeight); 
end

-- Register events
function broRegisterEvents()
	
	bro1AnchorFrame:RegisterEvent("ADDON_LOADED");
	bro1AnchorFrame:RegisterEvent("UNIT_HEALTH");
	bro1AnchorFrame:RegisterEvent("UNIT_MAXHEALTH");
	bro1AnchorFrame:RegisterEvent("PARTY_MEMBERS_CHANGED");
	
	bro2AnchorFrame:RegisterEvent("ADDON_LOADED");
	bro2AnchorFrame:RegisterEvent("UNIT_HEALTH");
	bro2AnchorFrame:RegisterEvent("UNIT_MAXHEALTH");
	bro2AnchorFrame:RegisterEvent("PARTY_MEMBERS_CHANGED");
	
	bro3AnchorFrame:RegisterEvent("ADDON_LOADED");
	bro3AnchorFrame:RegisterEvent("UNIT_HEALTH");
	bro3AnchorFrame:RegisterEvent("UNIT_MAXHEALTH");
	bro3AnchorFrame:RegisterEvent("PARTY_MEMBERS_CHANGED");
	
	bro4AnchorFrame:RegisterEvent("ADDON_LOADED");
	bro4AnchorFrame:RegisterEvent("UNIT_HEALTH");
	bro4AnchorFrame:RegisterEvent("UNIT_MAXHEALTH");
	bro4AnchorFrame:RegisterEvent("PARTY_MEMBERS_CHANGED");
end

-- Primary function calls
bro1CreateAnchor();
bro2CreateAnchor();
bro3CreateAnchor();
bro4CreateAnchor();

bro1CreateHealthBar();
bro2CreateHealthBar();
bro3CreateHealthBar();
bro4CreateHealthBar();
broRegisterEvents();

function broOnEvent()
	if (event == "UNIT_HEALTH" or event == "UNIT_MAXHEALTH") then
		bro1UpdateHealth();
		bro2UpdateHealth();
		bro3UpdateHealth();
		bro4UpdateHealth();
	elseif (event == "ADDON_LOADED") then
		bro1UpdateHealth();
		bro2UpdateHealth();
		bro3UpdateHealth();
		bro4UpdateHealth();
	elseif (event == "PARTY_MEMBERS_CHANGED") then
		bro1UpdateHealth();
		bro2UpdateHealth();
		bro3UpdateHealth();
		bro4UpdateHealth();
		--bro1Check();
	end
end

bro1AnchorFrame:SetScript("OnEvent", broOnEvent);
bro2AnchorFrame:SetScript("OnEvent", broOnEvent);
bro1AnchorFrame:SetScript("OnUpdate", broBars_OnUpdate);
bro2AnchorFrame:SetScript("OnUpdate", broBars_OnUpdate);